//
//  ExchangeIntegrateViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2019/1/18.
//  Copyright © 2019年 HCreater. All rights reserved.
//

#import "ExchangeIntegrateViewController.h"

@interface ExchangeIntegrateViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) ExchangeIntegrateViewModel* viewModel;

//IBOutlet
@property (weak, nonatomic) IBOutlet UITextField* numTF;
@property (weak, nonatomic) IBOutlet UIButton* nextBtn;

@end

@implementation ExchangeIntegrateViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    NSLog(@"viewDidLayoutSubviews");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
}

- (void)initUI
{
    @weakify(self);
    
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* willDisAppearSignal = [[self rac_signalForSelector:@selector(viewWillDisappear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [willDisAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        NSLog(@"viewDidAppear");
        
        [self.numTF resignFirstResponder];
    }];
    
    [[[[viewDidLayoutSVSignal take:1] zipWith:[willAppearSignal replay]] deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self)
         
         //重置 更新 frame
         [self _setupAllView];
     }];
    
#pragma mark -- bindViewModel
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    RAC(self.viewModel, coin) = [self.numTF rac_textSignal];
    
    [[self.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
     {
         @strongify(self);
         
         [self.viewModel.nextCmd execute:nil];
     }];
    [[self.viewModel.nextCmd.executionSignals.switchToLatest deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self);
         
         [self.navigationController popViewControllerAnimated:YES];
         
         UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
         [um updateUserInfo];
         
         [MBProgressHUD mh_showTips:@"兑换成功"];
     }];
    [[self.viewModel.nextCmd.errors deliverOnMainThread] subscribeNext:^(NSError* error)
     {
         @strongify(self);
         
         if ([error.domain isEqualToString:SUCommandErrorDomain])
         {
             [MBProgressHUD mh_showTips:error.userInfo[SUCommandErrorUserInfoKey]];
             return;
         }
         [MBProgressHUD mh_showErrorTips:error];
     }];
}

- (void)_setupAllView
{
    @weakify(self);
    
}

#pragma mark - 功能

#pragma mark - 跳转
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
